package com.dmjj.dmjjxt.controller;

import com.dmjj.common.base.BaseController;
import com.dmjj.common.base.LoginEntity;
import com.dmjj.common.result.ResultEnum;
import com.dmjj.common.result.ResultObject;
import com.dmjj.common.util.InsertCreatManInfo;
import com.dmjj.common.util.ListCopyToVOUtil;
import com.dmjj.common.util.PaginationVOUtil;
import com.dmjj.common.vo.ResultVO;
import com.dmjj.dmjjxt.dto.ProductSeriesDTO;
import com.dmjj.dmjjxt.service.ProductSeriesService;
import com.dmjj.dmjjxt.vo.ProductSeriesVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

/**
 * @Description 产品系列信息表控制器
 * @Author zhangxue
 * @Date 2019-04-11 14:13:51
 */

@RestController
@Api(tags = "产品系列信息表控制器")
@RequestMapping("productSeries")
public class ProductSeriesController extends BaseController {

    private static final Log LOGGER = LogFactory.getLog(ProductSeriesController.class);

    @Autowired
    private ProductSeriesService service;
    @Autowired
    private ResultObject resultObject;

    /**
	 * 产品系列信息表列表
     * @Author zhangxue
     * @Date 2019-04-11 14:13:51
	 * @Param
	 * @Return
	 */
    @ApiOperation(value = "产品系列信息表列表", response = ProductSeriesVO.class)
    @PostMapping("list4Page")
    public ResultVO<Object> list4Page(
            ProductSeriesVO entity,
            @ApiParam(value = "当前页", required = true) @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页显示条数", required = true) @RequestParam(value = "pageSize", defaultValue = "15") Integer pageSize
    ){
        try {
            ProductSeriesDTO entityDTO = new ProductSeriesDTO();
            BeanUtils.copyProperties(entity, entityDTO);
            List<ProductSeriesDTO> list = service.list4Page(entityDTO, pageNum, pageSize);
            return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), PaginationVOUtil.getPaginationVO(list, ProductSeriesDTO.class, pageNum));
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 产品系列信息表列表（无分页）
     * @Author zhangxue
     * @Date 2019-04-11 14:13:51
     * @Param
     * @Return
     */
    @ApiOperation(value = "产品系列信息表列表（无分页）", response = ProductSeriesVO.class)
    @PostMapping("listByCondition")
    public ResultVO<Object> listByCondition(ProductSeriesVO entity){
        try {
            ProductSeriesDTO entityDTO = new ProductSeriesDTO();
            BeanUtils.copyProperties(entity, entityDTO);
            List<ProductSeriesDTO> list = service.listByCondition(entityDTO);
            return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), ListCopyToVOUtil.getListToVO(list, ProductSeriesDTO.class));
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 产品系列信息表详细信息
     * @Author zhangxue
     * @Date 2019-04-11 14:13:51
     * @Param
     * @Return
     */
    @ApiOperation(value = "产品系列信息表详细信息", response = ProductSeriesVO.class)
    @PostMapping("queryOneByPk")
    public ResultVO<Object> queryOneByPk(@ApiParam(value = "id",required = true) @RequestParam(value = "id") Long id){
        try {
            ProductSeriesDTO entityDTO = service.queryOneByPk(id);
            ProductSeriesVO entity = new ProductSeriesVO();
            BeanUtils.copyProperties(entityDTO, entity);
            return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), entity);
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 保存产品系列信息表信息
     * @Author zhangxue
     * @Date 2019-04-11 14:13:51
     * @Param
     * @Return
     */
    @ApiOperation(value = "保存产品系列信息表信息", response = ResultVO.class)
    @PostMapping("save")
    public ResultVO<Object> save(ProductSeriesVO entity) {
        try {
            ProductSeriesDTO entityDTO = new ProductSeriesDTO();
            BeanUtils.copyProperties(entity, entityDTO);
            LoginEntity loginEntity = getUserInfo();
            if(loginEntity==null){
                return resultObject.error(ResultEnum.FAIL.getCode(), "token过期");
            }
            //获取创建人、创建时间
            entityDTO = InsertCreatManInfo.Update(entityDTO,loginEntity);
            if(service.save(entityDTO) > 0) {
                return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(),entityDTO);
            }else {
                return resultObject.error(ResultEnum.FAIL.getCode(), ResultEnum.FAIL.getMsg());
            }
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 删除产品系列信息表信息
     * @Author zhangxue
     * @Date 2019-04-11 14:13:51
     * @Param
     * @Return
     */
    @ApiOperation(value = "删除产品系列信息表信息", response = ResultVO.class)
    @PostMapping("delete")
    public ResultVO<Object> delete(@ApiParam(value = "id",required = true) @RequestParam(value = "id") Long id){
        try {
            if(service.delete(id) > 0) {
                return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg());
            }else {
                return resultObject.success(ResultEnum.FAIL.getCode(), ResultEnum.FAIL.getMsg());
            }
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 通过系列编号删除产品系列信息表信息
     * @Author zhangxue
     * @Date 2019-04-11 14:13:51
     * @Param
     * @Return
     */
    @ApiOperation(value = "通过系列编号删除产品系列信息表信息", response = ResultVO.class)
    @PostMapping("deleteBySeriesNo")
    public ResultVO<Object> deleteBySeriesNo(@ApiParam(value = "seriesno",required = true) @RequestParam(value = "seriesno") String seriesno){
        try {
            if(service.deleteBySeriesNo(seriesno) > 0) {
                return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg());
            }else {
                return resultObject.success(ResultEnum.FAIL.getCode(), ResultEnum.FAIL.getMsg());
            }
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

}